home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / MyAFPAsyncs.p < prev    next >
Encoding:
Text File  |  1997-01-10  |  4.6 KB  |  211 lines  |  [TEXT/CWIE]

  1. unit MyAFPAsyncs;
  2.  
  3. interface
  4.  
  5.     uses
  6.         AppleTalk;
  7.  
  8.     type
  9.         XPPXParamBlockRec = record
  10.                 qLink: XPPXParmBlkPtr;
  11.                 realResult: OSErr;
  12.                 pad: integer;
  13.                 your_completion: ProcPtr;
  14.                 my_completion: ProcPtr;
  15.                 pb: XPPParamBlock;
  16.             end;
  17.         XPPXParmBlkPtr = ^XPPXParamBlockRec;
  18.  
  19.     procedure AFPControlAsync (xppx: XPPXParmBlkPtr; comp: ProcPtr);
  20.     function XPPToXPPX( xpp: XPPParmBlkPtr ): XPPXParmBlkPtr;
  21.     procedure StartupAFPAsyncs;
  22.  
  23. implementation
  24.  
  25.     uses
  26.         Devices, PreserveA5, MyTypes, MyAssertions, MyStartup, MyCallProc, MyMemory;
  27.  
  28.     const
  29.         preheader_size = 16;
  30.     var
  31.         gMyCompletion: UniversalProcPtr;
  32.         afpque: QHdr;
  33.         tokenque: QHdr;
  34.         token: QElem;
  35.     
  36.     function XPPToXPPX( xpp: XPPParmBlkPtr ): XPPXParmBlkPtr;
  37.         var
  38.             xppx: XPPXParmBlkPtr;
  39.     begin
  40.         Assert( xpp <> nil );
  41.         xppx := XPPXParmBlkPtr(longint(xpp) - preheader_size);
  42.         Assert( xppx <> nil );
  43.         XPPToXPPX := xppx;
  44.     end;
  45.     
  46.     procedure PutToken;
  47.     begin
  48.         Assert( tokenque.qHead = nil );
  49.         Enqueue( @token, @tokenque );
  50.     end;
  51.     
  52.     function GetToken: boolean;
  53.         var
  54.             elem: QElemPtr;
  55.     begin
  56.         GetToken := false;
  57.         elem := tokenque.qHead;
  58.         if elem <> nil then begin
  59.             GetToken := Dequeue( elem, @tokenque ) = noErr;
  60.         end;
  61.     end;
  62.  
  63. {$ifc 0 & do_debug}
  64. {$definec ValidateState ValidateStateCode}
  65. {$elsec}
  66. { buggy compiler $ d efinec Assert(b)}
  67. {$definec ValidateState if false then begin end else begin end }
  68. {$endc}
  69.  
  70. {$ifc do_debug}
  71.     procedure ValidateStateCode;
  72.         var
  73.             xppx: XPPXParmBlkPtr;
  74.     begin
  75.         Assert( (tokenque.qHead = nil) <> (afpque.qHead = nil) ); { technically subject to race condition }
  76.         Assert( ((tokenque.qHead = nil) & (tokenque.qTail = nil)) | ((tokenque.qHead = @token) & (tokenque.qTail = @token)) );
  77.         xppx := XPPXParmBlkPtr(afpque.qHead);
  78.         while xppx <> nil do begin
  79.             Assert( xppx^.pb.ioResult = -9999 );
  80.             Assert( xppx^.realResult = inProgress );
  81.             Assert( xppx^.your_completion = nil );
  82.             xppx := xppx^.qLink;
  83.         end;
  84.     end;
  85. {$endc}
  86.     
  87.     procedure StartNextCommand;
  88.         var
  89.             xppx: XPPXParmBlkPtr;
  90.             junk: OSErr;
  91.     begin
  92.         xppx := XPPXParmBlkPtr( afpque.qHead );
  93.         if xppx <> nil then begin
  94.             Assert( xppx^.your_completion = nil );
  95.             junk := PBControlAsync( @xppx^.pb );
  96.         end else begin
  97.             PutToken;
  98.         end;
  99.     end;
  100.     
  101.     procedure MyCompletion(pbp: XPPParmBlkPtr);
  102.         var
  103.             xppx: XPPXParmBlkPtr;
  104.             comp: UniversalProcPtr;
  105.             junk: OSErr;
  106.     begin
  107.         xppx := XPPToXPPX( pbp );
  108.         Assert( afpque.qHead = QElemPtr(xppx) );
  109.         comp := xppx^.your_completion;
  110.         junk := Dequeue( QElemPtr(xppx), @afpque );
  111.         Assert( junk = noErr );
  112.         MTrash( xppx, preheader_size );
  113.  
  114.         xppx^.realResult := xppx^.pb.ioResult;
  115. {$ifc do_debug}
  116.         xppx^.pb.ioResult := -9999;
  117. {$endc}
  118.         if comp <> nil then begin
  119.             CallPascal04( pbp, comp );
  120.         end;
  121.         
  122.         StartNextCommand;
  123.  
  124.         ValidateState;
  125.     end;
  126.     
  127.     procedure AFPControlAsync (xppx: XPPXParmBlkPtr; comp: ProcPtr);
  128.         var
  129.             junk: OSErr;
  130.     begin
  131.         ValidateState;
  132.         
  133.         Assert( xppx <> nil );
  134.         Assert( gMyCompletion <> nil );
  135.         junk := Dequeue( QElemPtr(xppx), @afpque );
  136.         Assert( junk <> noErr );
  137.         Assert( xppx^.pb.qLink = nil );
  138.         xppx^.your_completion := comp;
  139.         xppx^.my_completion := gMyCompletion;
  140.         xppx^.pb.ioCompletion := gPreCompletionProc;
  141.         xppx^.pb.ioResult := inProgress;
  142. {$ifc do_debug}
  143.         xppx^.pb.ioResult := -9999;
  144. {$endc}        
  145.         xppx^.realResult := inProgress;
  146.         xppx^.pb.csCode := afpCall;
  147.         Enqueue( QElemPtr(xppx), @afpque );
  148.         if GetToken then begin
  149.             StartNextCommand;
  150.         end;
  151.     end;
  152.  
  153.     procedure IdleAFPAsyncs;
  154.     begin
  155.         ValidateState;
  156.     end;
  157.     
  158.     function InitAFPAsyncs( var msg: integer ): OSStatus;
  159.     begin
  160. {$unused(msg)}
  161.         afpque.qFlags := 0;
  162.         afpque.qHead := nil;
  163.         afpque.qTail := nil;
  164.         tokenque.qFlags := 0;
  165.         tokenque.qHead := nil;
  166.         tokenque.qTail := nil;
  167.         PutToken;
  168.         gMyCompletion := NewIOCompletionProc(@MyCompletion);
  169.         InitAFPAsyncs := noErr;
  170.     end;
  171.  
  172.     procedure StartupAFPAsyncs;
  173.     begin
  174.         StartupPreserveA5;
  175.         SetStartup( InitAFPAsyncs, IdleAFPAsyncs, 15, nil );
  176.     end;
  177.     
  178. end.
  179. (*
  180.     procedure AFPControlAsync (xppx: XPPXParmBlkPtr; comp: ProcPtr);
  181.         var
  182.             junk: OSErr;
  183.     begin
  184.         if comp <> nil then begin
  185.             xppx^.my_completion := comp;
  186.             xppx^.pb.ioCompletion := gPreCompletionProc;
  187.         end else begin
  188.             xppx^.pb.ioCompletion := nil;
  189.         end;
  190.         xppx^.pb.csCode := afpCall;
  191.         if xppx^.pb.qLink <> nil then begin
  192.             DebugStr('AFP Assert Failed;sc;hc');
  193.         end;
  194.         junk := PBControlAsync(@xppx^.pb);
  195.     end;
  196.  
  197.                 afp_in_progress := true;
  198.                 junk := Dequeue( QElemPtr(xppx), @afpque );
  199.                 Assert( junk = noErr );
  200.                 MTrash( xppx, 4 ); { qLink }
  201.                 Assert( current_request = nil );
  202.                 current_request := xppx;
  203.         current_request := nil;
  204.         if current_request <> nil then begin
  205.             Assert( current_request^.pb.ioResult = inProgress );
  206.         end;
  207.         Assert( current_request <> xppx );
  208.     
  209.  
  210.  
  211. *)